package GUI;
import javax.swing.JPanel;
import java.awt.FlowLayout;
import javax.swing.JLabel;
import java.awt.Font;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import javax.swing.SwingConstants;
import javax.swing.JComboBox;
import javax.swing.JButton;

import common.Statistics;

import MessagePacks.getStatisticReport;

import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Random;

/**
 *@author Juan Alapin - 324507516
 *@author Giora Yaffe - 036551281
 *@author Shachar Gritzman - 300174026
 *@author Nir Porat - 021515580
*/

public class ActivityReport extends MasterGUI {
	
	private static final long serialVersionUID = 1L;
	private MainGUI mainGui;
	private String[] days31;
	private String[] days30;
	private String[] days28;
	private String[] years;
	private String[] months;
	private String[] periods;
	private String DaysStart[];
	private String DaysEnd[];
	public JComboBox StartYearcomboBox;
	public JComboBox StartMonthcomboBox;
	public JComboBox StartDaycomboBox;
	public JComboBox EndYearcomboBox;
	public JComboBox EndMonthcomboBox;
	public JComboBox EndDaycomboBox;
	public JComboBox PeriodcomboBox;
	public JPanel reportPanel;
	public Statistics NumOfActive;
	public Statistics NumOfFrozen;
	public Statistics NumOfEnded;
	public Statistics NumOfTerminated;
	public JLabel lblActiveReqMedian;
	public 	JLabel lblActiveReqStdev;
	public JLabel lblActiveReqMean;
	public JLabel lblFrozenReqMedian;
	public JLabel lblFrozenReqStdev;
	public JLabel lblFrozenReqMean;
	public JLabel lblEndedReqMedian;
	public JLabel lblEndedReqStdev;
	public JLabel lblEndedReqMean;
	public JLabel lblTerminatedReqMedian;
	public JLabel lblTerminatedReqStdev;
	public JLabel lblTerminatedReqMean;
	public ArrayList<String> NumOfActive1;
	public ArrayList<String> NumOfFrozen1;
	public ArrayList<String> NumOfEnded1;
	public ArrayList<String> NumOfTerminated1;
	private getStatisticReport statReportPck;
	private getStatisticReport statReportPck1;
	private JLabel lblNumberOfActive2;
	
	public ActivityReport(MainGUI maingui) {
		super(maingui);
		this.mainGui=maingui;
		
		days31=new String[] {"01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31"};
		days30=new String[] {"01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30"};
		days28=new String[] {"01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28"};		
		months=new String[] {"01","02","03","04","05","06","07","08","09","10","11","12"};
		years=new String[] {"2000","2001","2002","2003","2004","2005","2006","2007","2008","2009","2010","2011","2012","2013","2014","2015","2016","2017","2018","2019","2020"};
		periods = new String[] {"Week","Month"};
		DaysStart = days28;
		DaysEnd = days28; 
		
		NumOfActive1=new ArrayList<String>();
		NumOfFrozen1=new ArrayList<String>();
		NumOfEnded1=new ArrayList<String>();
		NumOfTerminated1=new ArrayList<String>();
		NumOfActive1=AnalyzeData(NumOfActive1,30);
		NumOfFrozen1=AnalyzeData(NumOfFrozen1,30);
		NumOfEnded1=AnalyzeData(NumOfEnded1,30);
		NumOfTerminated1=AnalyzeData(NumOfTerminated1,30);
		
		JPanel titlePanel = new JPanel();
		titlePanel.setBounds(0, 0, 884, 40);
		getContentPane().add(titlePanel);
		titlePanel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		
		JLabel lblActivityReport = new JLabel("Activity Report");
		lblActivityReport.setFont(new Font("Dialog", Font.BOLD, 16));
		titlePanel.add(lblActivityReport);
		
		JPanel DatePanel = new JPanel();
		DatePanel.setBounds(0, 44, 884, 164);
		getContentPane().add(DatePanel);
		DatePanel.setLayout(null);
		
		JLabel lblStartYear = new JLabel("Year");
		lblStartYear.setBounds(266, 31, 36, 14);
		DatePanel.add(lblStartYear);
		lblStartYear.setHorizontalAlignment(SwingConstants.RIGHT);
		
		JLabel lblStartMonth = new JLabel("Month");
		lblStartMonth.setBounds(258, 56, 44, 14);
		DatePanel.add(lblStartMonth);
		lblStartMonth.setHorizontalAlignment(SwingConstants.RIGHT);
		
		JLabel lblStartDay = new JLabel("Day");
		lblStartDay.setBounds(269, 81, 33, 14);
		DatePanel.add(lblStartDay);
		lblStartDay.setHorizontalAlignment(SwingConstants.RIGHT);
		
		JLabel lblSelectStartDate = new JLabel("Select Start Date:");
		lblSelectStartDate.setBounds(292, 11, 96, 14);
		DatePanel.add(lblSelectStartDate);
		
		StartYearcomboBox = new JComboBox();
		StartYearcomboBox.setModel(new DefaultComboBoxModel(years));
		StartYearcomboBox.setBounds(312, 28, 64, 20);
		DatePanel.add(StartYearcomboBox);
		
		StartMonthcomboBox = new JComboBox();
		StartMonthcomboBox.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent e) {
				int month=Integer.parseInt((String)(StartMonthcomboBox.getSelectedItem()));
				switch(month)
				{
				case 1:
				case 3:
				case 5:
				case 7:
				case 8:
				case 10:
				case 12:DaysStart = days31;
					break;
				case 4:
				case 6:
				case 9:
				case 11:DaysStart = days30;
					break;
				case 2:DaysStart = days28;
					break;
				}
			}
		});
		StartMonthcomboBox.setModel(new DefaultComboBoxModel(months));
		StartMonthcomboBox.setBounds(312, 53, 64, 20);
		DatePanel.add(StartMonthcomboBox);
		
		StartDaycomboBox = new JComboBox();
		StartDaycomboBox.setModel(new DefaultComboBoxModel(DaysStart));
		StartDaycomboBox.setBounds(312, 78, 64, 20);
		DatePanel.add(StartDaycomboBox);
		
		JLabel lblSelectEndDate = new JLabel("Select End Date:");
		lblSelectEndDate.setBounds(505, 11, 96, 14);
		DatePanel.add(lblSelectEndDate);
		
		EndYearcomboBox = new JComboBox();
		EndYearcomboBox.setModel(new DefaultComboBoxModel(years));
		EndYearcomboBox.setBounds(519, 28, 64, 20);
		DatePanel.add(EndYearcomboBox);
		
		EndMonthcomboBox = new JComboBox();
		EndMonthcomboBox.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent e) {
				int month=Integer.parseInt((String)(StartMonthcomboBox.getSelectedItem()));
				switch(month)
				{
				case 1:
				case 3:
				case 5:
				case 7:
				case 8:
				case 10:
				case 12:DaysEnd = days31;
					break;
				case 4:
				case 6:
				case 9:
				case 11:DaysEnd = days30;
					break;
				case 2:DaysEnd = days28;
					break;
				}
			}
		});
		EndMonthcomboBox.setModel(new DefaultComboBoxModel(months));
		EndMonthcomboBox.setBounds(519, 53, 64, 20);
		DatePanel.add(EndMonthcomboBox);
		
		EndDaycomboBox = new JComboBox();
		EndDaycomboBox.setModel(new DefaultComboBoxModel(DaysEnd));
		EndDaycomboBox.setBounds(519, 78, 64, 20);
		DatePanel.add(EndDaycomboBox);
		
		JLabel lblEndYear = new JLabel("Year");
		lblEndYear.setHorizontalAlignment(SwingConstants.RIGHT);
		lblEndYear.setBounds(463, 31, 46, 14);
		DatePanel.add(lblEndYear);
		
		JLabel lblEndMonth = new JLabel("Month");
		lblEndMonth.setHorizontalAlignment(SwingConstants.RIGHT);
		lblEndMonth.setBounds(463, 56, 46, 14);
		DatePanel.add(lblEndMonth);
		
		JLabel lblEndDay = new JLabel("Day");
		lblEndDay.setHorizontalAlignment(SwingConstants.RIGHT);
		lblEndDay.setBounds(463, 81, 46, 14);
		DatePanel.add(lblEndDay);
		
		JButton btnGenerate = new JButton("Generate Report");
		btnGenerate.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				String periodStr = new String((String)(PeriodcomboBox.getSelectedItem()));
				if(true)
				{
				String start = new String(((String)(StartYearcomboBox.getSelectedItem()))+"-"+(String)(StartMonthcomboBox.getSelectedItem())+"-"+((String)(StartDaycomboBox.getSelectedItem())));
				String end = new String(((String)(EndYearcomboBox.getSelectedItem()))+"-"+(String)(EndMonthcomboBox.getSelectedItem())+"-"+((String)(EndDaycomboBox.getSelectedItem())));
				int startYear=Integer.parseInt(start.substring(0, 4));
				int startMonth=Integer.parseInt(start.substring(5, 7));
				int startDay=Integer.parseInt(start.substring(start.lastIndexOf('-')+1).trim());
				int endYear=Integer.parseInt(start.substring(0, 4));
				int endMonth=Integer.parseInt(start.substring(5, 7));
				int endDay=Integer.parseInt(start.substring(start.lastIndexOf('-')+1).trim());
						if(periodStr.equals("Week"))
						{
							statReportPck = new getStatisticReport(1,start,end);
							statReportPck1 = new getStatisticReport(1, start, end);
							statReportPck1.setNumOfActive(NumOfActive1);
							statReportPck1.setNumOfFreeze(NumOfFrozen1);
							statReportPck1.setNumOfActive(NumOfEnded1);
							statReportPck1.setNumOfFreeze(NumOfTerminated1);
						}
						else
						{
							statReportPck = new getStatisticReport(1,start,end);
							statReportPck1 = new getStatisticReport(1, start, end);
							statReportPck1.setNumOfActive(NumOfActive1);
							statReportPck1.setNumOfFreeze(NumOfFrozen1);
							statReportPck1.setNumOfActive(NumOfEnded1);
							statReportPck1.setNumOfFreeze(NumOfTerminated1);
						}
						//Send Message
						mainGui.client.handleMessageFromClientUI(statReportPck);
						statReportPck = (getStatisticReport)(mainGui.client.getMessage());
						ShowReport();
					}
				}
		});
		btnGenerate.setBounds(384, 132, 139, 23);
		DatePanel.add(btnGenerate);
		
		PeriodcomboBox = new JComboBox();
		PeriodcomboBox.setModel(new DefaultComboBoxModel(periods));
		PeriodcomboBox.setBounds(418, 106, 83, 20);
		DatePanel.add(PeriodcomboBox);
		
		JLabel lblTimePeriod = new JLabel("Per Time Period:");
		lblTimePeriod.setHorizontalAlignment(SwingConstants.RIGHT);
		lblTimePeriod.setBounds(312, 109, 104, 14);
		DatePanel.add(lblTimePeriod);

		reportPanel = new JPanel();
		reportPanel.setBounds(193, 212, 681, 379);
		getContentPane().add(reportPanel);
		reportPanel.setLayout(null);
		reportPanel.setVisible(true);
		
		reportPanel.setVisible(true);
		JLabel lblMedians = new JLabel("Medians");
		lblMedians.setFont(new Font("Dialog", Font.BOLD, 14));
		lblMedians.setBounds(10, 11, 84, 24);
		reportPanel.add(lblMedians);
		
		JLabel lblNumberOfActive = new JLabel("Number Of Active Requests:");
		lblNumberOfActive.setBounds(10, 46, 180, 14);
		reportPanel.add(lblNumberOfActive);
		
		lblActiveReqMedian = new JLabel();
		lblActiveReqMedian.setBounds(200, 46, 46, 14);
		reportPanel.add(lblActiveReqMedian);
		
		JLabel lblNumberOfFrozen = new JLabel("Number Of Frozen Requests:");
		lblNumberOfFrozen.setBounds(10, 71, 180, 14);
		reportPanel.add(lblNumberOfFrozen);
		
		lblFrozenReqMedian = new JLabel();
		lblFrozenReqMedian.setBounds(200, 71, 46, 14);
		reportPanel.add(lblFrozenReqMedian);
		
		JLabel lblNumberOfEnded = new JLabel("Number Of Ended Requests:");
		lblNumberOfEnded.setBounds(10, 96, 180, 14);
		reportPanel.add(lblNumberOfEnded);
		
		lblEndedReqMedian = new JLabel();
		lblEndedReqMedian.setBounds(200, 96, 46, 14);
		reportPanel.add(lblEndedReqMedian);
		
		JLabel lblNumberIfTerminated = new JLabel("Number If Terminated Requests:");
		lblNumberIfTerminated.setBounds(10, 121, 180, 14);
		reportPanel.add(lblNumberIfTerminated);
		
		lblTerminatedReqMedian = new JLabel();
		lblTerminatedReqMedian.setBounds(200, 121, 46, 14);
		reportPanel.add(lblTerminatedReqMedian);
		
		JLabel lblStandardDeviation = new JLabel("Standard Deviation");
		lblStandardDeviation.setFont(new Font("Dialog", Font.BOLD, 14));
		lblStandardDeviation.setBounds(319, 16, 164, 14);
		reportPanel.add(lblStandardDeviation);
		
		lblNumberOfActive2 = new JLabel("Number Of Active Requests:");
		lblNumberOfActive2.setBounds(319, 46, 180, 14);
		reportPanel.add(lblNumberOfActive2);
		
		JLabel lblNumberOfFrozen2 = new JLabel("Number Of Frozen Requests:");
		lblNumberOfFrozen2.setBounds(319, 71, 180, 14);
		reportPanel.add(lblNumberOfFrozen2);
		
		JLabel lblNumberOfEnded2 = new JLabel("Number Of Ended Requests:");
		lblNumberOfEnded2.setBounds(319, 96, 164, 14);
		reportPanel.add(lblNumberOfEnded2);
		
		JLabel lblNumberOfTerminated2 = new JLabel("Number If Terminated Requests:");
		lblNumberOfTerminated2.setBounds(319, 121, 164, 14);
		reportPanel.add(lblNumberOfTerminated2);
		
		lblActiveReqStdev = new JLabel();
		lblActiveReqStdev.setBounds(509, 46, 46, 14);
		reportPanel.add(lblActiveReqStdev);
		
		lblFrozenReqStdev = new JLabel();
		lblFrozenReqStdev.setBounds(509, 71, 46, 14);
		reportPanel.add(lblFrozenReqStdev);
		
		lblEndedReqStdev = new JLabel();
		lblEndedReqStdev.setBounds(509, 96, 46, 14);
		reportPanel.add(lblEndedReqStdev);
		
		lblTerminatedReqStdev = new JLabel();
		lblTerminatedReqStdev.setBounds(509, 121, 46, 14);
		reportPanel.add(lblTerminatedReqStdev);
		
		JLabel lblMeans = new JLabel("Means");
		lblMeans.setFont(new Font("Dialog", Font.BOLD, 14));
		lblMeans.setBounds(10, 171, 84, 14);
		reportPanel.add(lblMeans);
		
		JLabel lblNumberOfActive3 = new JLabel("Number Of Active Requests:");
		lblNumberOfActive3.setBounds(10, 196, 180, 14);
		reportPanel.add(lblNumberOfActive3);
		
		JLabel lblNumberOfFrozen3 = new JLabel("Number Of Frozen Requests:");
		lblNumberOfFrozen3.setBounds(10, 221, 180, 14);
		reportPanel.add(lblNumberOfFrozen3);
		
		JLabel lblNumberOfEnded3 = new JLabel("Number Of Ended Requests:");
		lblNumberOfEnded3.setBounds(10, 246, 180, 14);
		reportPanel.add(lblNumberOfEnded3);
		
		JLabel lblNumberIfTerminated3 = new JLabel("Number If Terminated Requests:");
		lblNumberIfTerminated3.setBounds(10, 271, 180, 14);
		reportPanel.add(lblNumberIfTerminated3);
		
		lblActiveReqMean = new JLabel();
		lblActiveReqMean.setBounds(200, 196, 46, 14);
		reportPanel.add(lblActiveReqMean);
		
		lblFrozenReqMean = new JLabel();
		lblFrozenReqMean.setBounds(200, 221, 46, 14);
		reportPanel.add(lblFrozenReqMean);
		
		lblEndedReqMean = new JLabel();
		lblEndedReqMean.setBounds(200, 246, 46, 14);
		reportPanel.add(lblEndedReqMean);
		
		lblTerminatedReqMean = new JLabel();
		lblTerminatedReqMean.setBounds(200, 271, 46, 14);
		reportPanel.add(lblTerminatedReqMean);
			}

public ArrayList<String> AnalyzeData(ArrayList<String> AL,int len){
	//int n = rand.nextInt(numOfWrks) + 1;
	int i;
	Random rand = new Random();
	for(i=0;i<len;i++)
		AL.add(Integer.toString(rand.nextInt(len)+1));
	return AL;
}


public void ShowReport(){
	lblActiveReqMedian.setText(Double.toString(mainGui.analyzer()));
	lblActiveReqStdev.setText(Double.toString(mainGui.analyzer()));
	lblActiveReqMean.setText(Double.toString(mainGui.analyzer()));
	lblFrozenReqMedian.setText(Double.toString(mainGui.analyzer()));
	lblFrozenReqStdev.setText(Double.toString(mainGui.analyzer()));
	lblFrozenReqMean.setText(Double.toString(mainGui.analyzer()));
	lblEndedReqMedian.setText(Double.toString(mainGui.analyzer()));
	lblEndedReqStdev.setText(Double.toString(mainGui.analyzer()));
	lblEndedReqMean.setText(Double.toString(mainGui.analyzer()));
	lblTerminatedReqMedian.setText(Double.toString(mainGui.analyzer()));
	lblTerminatedReqStdev.setText(Double.toString(mainGui.analyzer()));
	lblTerminatedReqMean.setText(Double.toString(mainGui.analyzer()));
}

	
}
